<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Config;
use think\Db;
use think\exception\HttpException;
use think\Loader;

class ProductResource extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();

        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            if (!$data['pid'] || !$data['type']) {
                return ajax_return_adv_error("缺少参数");
            }

            // 验证
            if (class_exists($validateClass = Loader::parseClass(Config::get('app.validate_path'), 'validate', $controller))) {
                $validate = new $validateClass();
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }

            // 更新数据
            if (
                class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $this->parseCamelCase($controller)))
                || class_exists($modelClass = Loader::parseClass(Config::get('app.model_path'), 'model', $controller))
            ) {
                // 使用模型更新，可以在模型中定义更高级的操作
                $model = new $modelClass();
                if ($model->where(['id'=>$data['id']])->find()) {
                    $ret = $model->isUpdate(true)->save($data, ['id' => $data['id']]);
                } else {
                    $ret = $model->isUpdate(false)->save($data);
                }
            } else {
                // 简单的直接使用db更新
                Db::startTrans();
                try {
                    $model = Db::name($this->parseTable($controller));
                    $where = ['pid'=>$data['pid'],'type'=>$data['type']];
                    $data['content'] = json_encode($data['content'], JSON_UNESCAPED_UNICODE);
                    if ($model->where($where)->find()) {
                        $ret = $model->where($where)->update($data);
                    } else {
                        $ret = $model->insert($data);
                    }

                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();

                    return ajax_return_adv_error($e->getMessage());
                }
            }

            return ajax_return_adv("操作成功");
        } else {
            // 编辑
            $pid = $this->request->param('pid');
            $type = $this->request->param('type');
            if (!$pid || !$type) {
                throw new HttpException(404, "缺少参数");
            }
            $vo = $this->getModel($controller)->where(['pid'=>$pid,'type'=>$type])->find();
            if (!$vo) {
                //throw new HttpException(404, '该记录不存在');
            }
            $vo['content'] = json_decode($vo['content'], true);
            $this->view->assign("vo", $vo);

            return $this->view->fetch();
        }
    }
}
